package com.tj.shopping.web.controller;

import com.tj.shopping.entity.Goodsdetail;
import com.tj.shopping.service.IGoodsdetailService;
import com.tj.shopping.service.impl.GoodSdetailServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/goodsdetail")
public class GoodsdetailController extends BaseController {

    private IGoodsdetailService service = new GoodSdetailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("add".equals(op)) {
            doAdd(req, resp);
        } else if ("searchGoodsDetail".equals(op)) {
            dosearchGoodsDetail(req, resp);
        } else {
            resp.getWriter().print("no such Method....");
        }
    }

    protected void dosearchGoodsDetail(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Map<String, Object> map = parseRequest(req);
        Map<String, Object> map1 = service.searchGoodsDetail(map);
        System.out.println(map1);
        printToJson(resp, map1);
    }

    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Goodsdetail goodsdetail = parseRequest(req, Goodsdetail.class);
        System.out.println(goodsdetail);
        int result = service.add(goodsdetail);
        System.out.println(result);
        printToJson(resp, result);
    }
}

